// store/index.js
import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    books: [] // 初始化书籍列表为空数组
  },
  mutations: {
    ADD_BOOK(state, newBook) {
      state.books.push(newBook);
    },
    REMOVE_BOOK(state, index) {
      state.books.splice(index, 1);
    }
  },
  actions: {
    addBookToStore({ commit }, newBook) {
      commit('ADD_BOOK', newBook);
    },
    removeBookFromStore({ commit }, index) {
      commit('REMOVE_BOOK', index);
    }
  },
  getters: {
    allBooks: state => state.books
  }
});